package edu.lagou.controller;

import edu.lagou.service.AuthcodeService;
import edu.lagou.service.CodeService;
import edu.lagou.service.EmailService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/code")
public class CodeController {

    @Autowired
    private AuthcodeService authcodeService;

    @Reference
    private EmailService emailService;

    @RequestMapping("/create/{email}")
    public boolean createCode(@PathVariable String email) {
        boolean result = false;
        String cd = authcodeService.createCode(email);
        result = emailService.sendCode(email, cd);
        if (cd.length() > 0) {
            result = true;
        }

        System.out.println(">>>>The email: " + email + " return: " + cd);
        return  result;
    }
}
